package com.bdcom.dialog;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.comp.CanvasPanel;
import com.bdcom.comp.ScaleLabelPanel;
import com.bdcom.comp.SignSlider;
import com.bdcom.model.IBarrier;
import com.bdcom.model.IDevice;
import com.bdcom.util.MapRender;
import com.bdcom.util.ProgressWindow;
import com.bdcom.util.PlannerUtil;
@Component
public class RenderDialog extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 主画布
	private JPanel mapPanel = null;
	@Autowired
	private MapRender mapRender;
	@Autowired
	private CanvasPanel canvasPanel;
	@Autowired
	private ScaleLabelPanel scaleLabelPanel;
	@Autowired
	private SignSlider signSlider;
	@Autowired
	private ProgressWindow progressWindow;
	// 信号强度图
	private BufferedImage mapImage = null;

	public RenderDialog() {

		JMenuBar mbar = new JMenuBar();

		JButton btn = new JButton(PlannerUtil.getString("save image"));

		btn.addActionListener(this);

		mbar.add(btn);

		setJMenuBar(mbar);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	/**
	 * 调用初始化方法 进行无线信号覆盖图的 渲染与展示
	 */
	public void init() {
		
		progressWindow.setLocationRelativeTo(null);
		
		JFrame me=this;

		getContentPane().removeAll();
		
		new Thread(new Runnable() {
			
			public void run() {
				
				progressWindow.setVisible(true);
				
				me.setVisible(false);
				
				int w = canvasPanel.getCanvasModel().getBackImg().getWidth();

				int h = canvasPanel.getCanvasModel().getBackImg().getHeight();

				mapRender.setCanvasModel(canvasPanel.getCanvasModel());

				mapImage = mapRender.render();

				double scale=canvasPanel.getCanvasModel().getRoomPercent();

				mapPanel = new JPanel() {
					
					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					protected void paintComponent(Graphics g) {

						super.paintComponent(g);

						Graphics2D g2d = (Graphics2D) g;
						
						g2d.scale(scale, scale);
						
						g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
						
						//Composite oldComp = g2d.getComposite();

						// 背景
						if (canvasPanel.getCanvasModel().getBackImg() != null) {

							g2d.drawImage(canvasPanel.getCanvasModel().getBackImg(), 0, 0, w, h, null);

						}
						
						// 障碍物
						for (IBarrier barrier : canvasPanel.getCanvasModel().getBarriers()) {

							barrier.drawBarrier(g2d, false);

						}
						//g2d.setComposite(oldComp);
						// 画设备
						for (IDevice d : canvasPanel.getCanvasModel().getDevs()) {
							
							try {
								d.drawDevice(g2d, false);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}
						// 信号图
						Composite translucent = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.618f);

						g2d.setComposite(translucent);

						g2d.drawImage(mapImage, 0, 0, w, h, null);

						//指示图标
						g2d.translate(30, 15);

						scaleLabelPanel.print(g2d);

						g2d.translate(0, h - 80);

						signSlider.print(g2d);
						
					}
				};

				mapPanel.setPreferredSize(new Dimension((int)(w*scale), (int)(h*scale)));

				JScrollPane jScrollPane = new JScrollPane(mapPanel);

				getContentPane().add(jScrollPane);

				pack();
				
				setTitle(canvasPanel.getCanvasModel().getProjectName());
				
				progressWindow.setVisible(false);
				
				setLocationRelativeTo(null);
				
				me.setState(NORMAL);
				
				me.setVisible(true);
			}
		}).start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		BufferedImage image = new BufferedImage(mapPanel.getWidth(), mapPanel.getHeight(), BufferedImage.TYPE_INT_RGB);

		Graphics2D g2 = (Graphics2D) image.getGraphics();

		mapPanel.paintAll(g2);

		JFileChooser chooser = new JFileChooser(new File("."));

		FileNameExtensionFilter filter = new FileNameExtensionFilter("图片(*.jpeg)", ".jpeg");

		chooser.setFileFilter(filter);

		if (chooser.showDialog(null, PlannerUtil.getString("save")) == JFileChooser.APPROVE_OPTION) {

			File oFile;

			if (!chooser.getSelectedFile().getName().endsWith(".jpeg")) {

				oFile = new File(chooser.getSelectedFile().getAbsolutePath() + ".jpeg");

			} else {

				oFile = chooser.getSelectedFile();

			}

			try {

				ImageIO.write(image, "jpeg", oFile);// 保存图像文件

			} catch (IOException ex) {

				ex.printStackTrace();

				JOptionPane.showMessageDialog(null, PlannerUtil.getString("save faild"));

				return;
			}

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("save success"));

		}

	}

}
